﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.ComponentModel;

public partial class UserControl_TimeControl : UserControl
{
    /// <summary>
    /// 1表示年月选择，2表示年月日选择.3表示带时分秒
    /// </summary>    
    public int SeleType
    {
        get
        {
            object v = ViewState["SeleType"];
            if (null != v)
            {
                return (int)v;
            }
            else
            {
                return 2;// default(int);
            }
        }
        set
        {
            ViewState["SeleType"] = value;
            phm.Visible = value > 0;
            phd.Visible = value > 1;
            DropDownList4.Visible = DropDownList5.Visible = (value > 2);
        }
    }
    public bool ReadOnly
    {
        set {
            TextBox1.ReadOnly = value;
            DropDownList3.Enabled = (!value);
            DropDownList4.Enabled = (!value);
            DropDownList5.Enabled = (!value);
            DropDownList1.Enabled = (!value);
            DropDownList2.Enabled = (!value);
        }
    }
    public bool IsInputYear
    {
        set {
            TextBox1.Visible = value;
            DropDownList3.Visible = (!value);
        }
        get { return TextBox1.Visible; }
    }
    public DateTime Value
    {
        get
        {
            if (DropDownList1.SelectedIndex == -1 || DropDownList2.SelectedIndex == -1) return DateTime.Now;

            int t = SeleType;
            int y, m, d, h, mi;// y=DateTime.Now.Year,m=DateTime.Now.Month,d=DateTime.Now.Day;
            d = 1; h = mi = 0;

            if (IsInputYear)
            {
                if (!int.TryParse(TextBox1.Text.TrimEnd('年'), out y))
                {
                    y = DateTime.Now.Year;
                    TextBox1.Text = DateTime.Now.Year.ToString();
                }
            }
            else
                y = int.Parse(DropDownList3.Text.TrimEnd('年'));
            

            m = int.Parse(DropDownList1.Text.Substring(0, DropDownList1.Text.Length - 1));
            d = t > 1 ? int.Parse(DropDownList2.Text.Substring(0, DropDownList2.Text.Length - 1)) : 1;
            h = t > 2 ? int.Parse(DropDownList4.Text.Substring(0, DropDownList4.Text.Length - 1)) : 0;
            mi = t > 2 ? int.Parse(DropDownList5.Text.Substring(0, DropDownList5.Text.Length - 1)) : 0;

            try
            {
                return new DateTime(y, m, d, h, mi, 0);
            }
            catch
            {               
                return new DateTime(y, m + 1, 1, h, mi, 0);
            }

        }
        set
        {
            TextBox1.Text = value.Year.ToString()+"年";
            DropDownList1.Text = value.Month.ToString() + "月";
            DropDownList2.Text = value.Day.ToString() + "日";
            DropDownList3.Text = value.Year.ToString()+"年";
            DropDownList4.Text = value.Hour.ToString("00") + "时";
            DropDownList5.Text = value.Minute.ToString("00") + "分";
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
    }
    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        InitView();
    }
    public void InitView()
    {
        if (DropDownList3.Items.Count == 0)
        {
            for (int i = 0; i < 12; i++) DropDownList3.Items.Add((DateTime.Now.Year - 10 + i).ToString()+"年");
            for (int i = 1; i < 13; i++) DropDownList1.Items.Add(i.ToString() + "月");
            for (int i = 1; i < 32; i++) DropDownList2.Items.Add(i.ToString() + "日");

            for (int i = 0; i < 24; i++) DropDownList4.Items.Add(i.ToString("00") + "时");
            for (int i = 0; i < 60; i++) DropDownList5.Items.Add(i.ToString("00") + "分");
        }
    }
}
